package main

import "fmt"

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func lengthOfLongestSubstring(s string) int {
	n, maxSubStrLen, lastIdx, tempSubStrLen := len(s), -1, 0, 0
	mapLastIdx := map[byte]int{}
	for i := 0; i < n; i++ {
		tempSubStrLen++
		if idx, ok := mapLastIdx[s[i]]; ok {
			maxSubStrLen = max(maxSubStrLen, i-idx)
			maxSubStrLen = max(maxSubStrLen, tempSubStrLen-1)
			fmt.Println(maxSubStrLen)
			tempSubStrLen = 0
			lastIdx = i
		}
		mapLastIdx[s[i]] = i
	}
	return max(maxSubStrLen, n-lastIdx)
}

func main() {
	//fmt.Println(lengthOfLongestSubstring("abcabcbb"))
	//fmt.Println(lengthOfLongestSubstring("aab"))
	//fmt.Println(lengthOfLongestSubstring("cdd"))
	fmt.Println(lengthOfLongestSubstring("abba"))
}
